package com.meixiang.activity.account.managers;

import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.dialog.ManageBankDialogFragment;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.TitleView;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 银行卡管理界面
 *
 * @author zys
 */
public class ManageBankCardActivity extends BaseActivity {

//

    @Bind(R.id.title)
    TitleView title;
    private TextView tvAddBank;
    private ImageView ivBankLogo;
    private TextView tvBankName;
    private TextView tvCardNum;

    private boolean isBind = true;

    ViewStub vsNotBind;
    @Bind(R.id.vs_has_bind)
    ViewStub vsHasBind;
     private String  cancelType;
    String bankState;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_manage_bank_card);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initTitle() {
        // 如果已经绑定了银行卡则显示管理按钮
        if (isBind) {
            title.setTitle("银行卡");
            title.showRightButton(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                }
            });
            title.setLeftImageButton(R.mipmap.back);
            title.showLeftButton(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            title.setRightTextButton("管理");
            title.showRightButton(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ManageBankDialogFragment.newInstance().show(getSupportFragmentManager(), ManageBankDialogFragment.TAG);
                }
            });
        } else {
            setTitle("银行卡");
        }
    }

    @Override
    protected void initView() {





    }

    @Override
    protected void onResume() {
        try {
            cancelType =this.getIntent().getStringExtra("cancel_bind");
            AbToastUtil.showToast(ManageBankCardActivity.this,"00000"+cancelType);

            if (cancelType.equals("2")){//// 取消绑定
                if (isBind) {
                    // 未绑定
                    vsNotBind.inflate();
                    tvAddBank = (TextView) findViewById(R.id.tv_add_bank);
                    tvAddBank.setOnClickListener(this);
                    vsNotBind.invalidate();
                }
            }
        }catch (Exception e){
            e.getStackTrace();
        }
        super.onResume();
    }

    @Override
    protected void setData() {
        getBankListSelect();
        if (isBind) {
            // 已绑定
            vsHasBind.inflate();
            tvBankName = (TextView) findViewById(R.id.edtTxt_bank_name);
            tvCardNum = (TextView) findViewById(R.id.edtTxt_card_num);
            ivBankLogo = (ImageView) findViewById(R.id.iv_bank_logo);

        } else {
            // 未绑定
            vsNotBind.inflate();
            tvAddBank = (TextView) findViewById(R.id.tv_add_bank);
            tvAddBank.setOnClickListener(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }


    @Override
    public void onClick(View v) {
//        // 判断是否连续点击
//        if (Tool.isFastDoubleClick()) return;
//        Intent intent;
//        switch (v.getId()) {
//            // 添加银行卡
//            case R.id.tv_add_bank:
//                intent = new Intent(this, BindBankCardActivity.class);
//                startActivity(intent);
//                break;
//            // 管理
//            case R.id.title_right:
//                ManageBankDialogFragment.newInstance().show(getSupportFragmentManager(), ManageBankDialogFragment.TAG);
//                break;
//        }
  }
    /*
   *
   * 选择银行卡
   * **/
    private void getBankListSelect(){
        HttpUtils.post(Config.MY_ACCOUNT_BANKINDEX,null, new HttpCallBack(ManageBankCardActivity.this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {


                    try {
                        bankState=result.toString();
//                        "imageUrl": "银行图片",
//                                "bankName": "农业银行-储蓄卡",
//                                "bankCode": "**** **** **** 1456"
                        if (code.equals("0")){
                            if(result.toString().equals("{}")){
                                AbToastUtil.showToast(ManageBankCardActivity.this,message);
                                return;
                            }
                            tvBankName.setText(result.getString("bankName"));
                            tvCardNum.setText(result.getString("bankCode"));
                            AbToastUtil.showToast(ManageBankCardActivity.this,message);
                        }else {
                            AbToastUtil.showToast(ManageBankCardActivity.this,message);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(ManageBankCardActivity.this,message);
            }
        });



    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        if(resultCode == 1)
//        {
//            getBankBind();
//         AbToastUtil.showToast(ManageBankCardActivity.this,requestCode+"");
//        }
//        super.onActivityResult(requestCode, resultCode, data);
//    }






}
